package com.zc.im.controller;

import com.alibaba.fastjson.JSONObject;
import com.zc.im.model.im.group.ZcImGroupSign;
import com.zc.im.service.im.group.ImGroupSignService;
import com.zc.im.util.JsonUtils;
import com.zc.im.util.ResponseJson;
import com.zc.im.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 签到controller
 * 传入的json{"groupId":"2","signAddrModel":"company","signType":"sign","signPics":"我的头像","signContent":"","userId":"1278028"}
 * Created by wangcc on 2017/7/8.
 */
@Controller
@RequestMapping(value = "/groupsign")
public class ImGroupSignController{

    @Autowired
    private ImGroupSignService imGroupSignService;
    private Logger logger = Logger.getLogger(ImGroupSignController.class);

    /**
     * 加入签到信息方法
     * 传入的json{"groupId":"2","signAddrModel":"company","signType":"sign","signPics":"我的头像","signContent":"","userId":"1278028"}
     * @param savegroupsign
     * @return
     */
    @RequestMapping(value = "/insertgroupsign",method = RequestMethod.POST)
    public ResponseJson insertGroupsign(String savegroupsign){
        ResponseJson responseJson = new ResponseJson();
        if (StringUtils.isEmpty(savegroupsign)) {
            return responseJson;
        }
        ZcImGroupSign zcImGroupSign = JsonUtils.strToClass(savegroupsign,ZcImGroupSign.class);
        Integer key=null;
        try {
            key = imGroupSignService.saveGroupSign( zcImGroupSign);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("key",key);
        responseJson.setDataMap(dataMap);
        return  responseJson;
    }

    /**
     * 通过周月年检索迟到和早退
     * 传入的json{"companyId":"2","startTime":"2017-07-01","EndTime":"2017-07-30","signType":"sign_out"}
     * @param listLateGroupsing
     * @return
     */
    @RequestMapping(value = "/listgroupsign",method = RequestMethod.POST)
    public ResponseJson listGroupsign(String listLateGroupsing) {
       ResponseJson  responseJson = new ResponseJson();
       if (StringUtils.isEmpty(listLateGroupsing)) {
           return responseJson;
       }
       JSONObject jsonObject = JsonUtils.strToJsonObj(listLateGroupsing);
       List<ZcImGroupSign> listZcImGroupSign = imGroupSignService
       .listLateGroupSign(Integer.parseInt( jsonObject.getString("companyId") )
               ,jsonObject.getString("startTime")
               ,jsonObject.getString("EndTime")
               ,jsonObject.getString("signType") );
       Map<String , Object> dataMap = new HashMap<>();
       dataMap.put("key",JsonUtils.objToStr( listZcImGroupSign ));
       responseJson.setDataMap(dataMap);
       return responseJson;
}



}
